{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<frozen importlib._bootstrap>:219: RuntimeWarning: compiletime version 3.6 of module 'pycocotools._mask' does not match runtime version 3.8\n",
      "<frozen importlib._bootstrap>:219: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 864 from C header, got 880 from PyObject\n",
      "WARNING: OMP_NUM_THREADS set to 14, not 1. The computation speed will not be optimized if you use data parallel. It will fail if this PaddlePaddle binary is compiled with OpenBlas since OpenBlas does not support multi-threads.\n",
      "PLEASE USE OMP_NUM_THREADS WISELY.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/clsdata(1031)/cache/cls_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/clsdata(1031)/Cls/annotations/instancesCls(1031)_val2019.json\n",
      "Done (t=0.13s)\n",
      "creating index...\n",
      "index created!\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/clsdata(1031)/Cls/annotations/instancesCls(1031)_val2019.json\n",
      "Done (t=0.04s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetCls\n",
      "use kp\n",
      "total parameters: 199649452\n",
      "loading parameters...\n",
      "loading model from data/clsdata(1031)/cache/nnet/CornerNetCls/CornerNetCls_50000.pkl\n",
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/bardata(1031)/cache/chart_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/bardata(1031)/bar/annotations/instancesBar(1031)_val2019.json\n",
      "Done (t=0.65s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetPureBar\n",
      "use kp pure\n",
      "total parameters: 198592652\n",
      "loading parameters...\n",
      "loading model from data/bardata(1031)/cache/nnet/CornerNetPureBar/CornerNetPureBar_50000.pkl\n",
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/piedata(1008)/cache/pie_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/piedata(1008)/pie/annotations/instancesPie(1008)_val2019.json\n",
      "Done (t=0.03s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetPurePie\n",
      "use kp pure pie\n",
      "total parameters: 198592652\n",
      "loading parameters...\n",
      "loading model from data/piedata(1008)/cache/nnet/CornerNetPurePie/CornerNetPurePie_50000.pkl\n",
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/linedata(1028)/cache/line_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/linedata(1028)/line/annotations/instancesLine(1023)_val2019.json\n",
      "Done (t=0.05s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetLine\n",
      "use kp\n",
      "total parameters: 198592138\n",
      "loading parameters...\n",
      "loading model from data/linedata(1028)/cache/nnet/CornerNetLine/CornerNetLine_50000.pkl\n",
      "loading parameters at iteration: 20000\n",
      "loading from cache file: data/linedata(1028)/cache/line_real_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/linedata(1028)/line/annotations/instancesLineClsReal(1119)_val2019.json\n",
      "Done (t=0.04s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetLineClsReal\n",
      "use kp pure\n",
      "total parameters: 188750476\n",
      "loading parameters...\n",
      "loading model from data/linedata(1028)/cache/nnet/CornerNetLineClsReal/CornerNetLineClsReal_20000.pkl\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "import os\n",
    "import json\n",
    "import torch\n",
    "import pprint\n",
    "import argparse\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.use(\"Agg\")\n",
    "import cv2\n",
    "from tqdm import tqdm\n",
    "from config import system_configs\n",
    "from nnet.py_factory import NetworkFactory\n",
    "from db.datasets import datasets\n",
    "import importlib\n",
    "from RuleGroup.Cls import GroupCls\n",
    "from RuleGroup.Bar import GroupBar\n",
    "from RuleGroup.LineQuiry import GroupQuiry\n",
    "from RuleGroup.LIneMatch import GroupLine\n",
    "from RuleGroup.Pie import GroupPie\n",
    "import math\n",
    "from paddleocr import PaddleOCR\n",
    "from PIL import Image, ImageDraw, ImageFont\n",
    "torch.backends.cudnn.benchmark = False\n",
    "import requests\n",
    "import time\n",
    "import re\n",
    "def parse_args():\n",
    "    parser = argparse.ArgumentParser(description=\"Test CornerNet\")\n",
    "    parser.add_argument(\"--cfg_file\", dest=\"cfg_file\", help=\"config file\", default=\"CornerNetLine\", type=str)\n",
    "    parser.add_argument(\"--testiter\", dest=\"testiter\",\n",
    "                        help=\"test at iteration i\",\n",
    "                        default=50000, type=int)\n",
    "    parser.add_argument(\"--split\", dest=\"split\",\n",
    "                        help=\"which split to use\",\n",
    "                        default=\"validation\", type=str)\n",
    "    parser.add_argument('--cache_path', dest=\"cache_path\", type=str)\n",
    "    parser.add_argument('--result_path', dest=\"result_path\", type=str)\n",
    "    parser.add_argument('--tar_data_path', dest=\"tar_data_path\", type=str)\n",
    "    parser.add_argument(\"--suffix\", dest=\"suffix\", default=None, type=str)\n",
    "    parser.add_argument(\"--debug\", action=\"store_true\")\n",
    "    parser.add_argument(\"--data_dir\", dest=\"data_dir\", default=\"data/linedata(1028)\", type=str)\n",
    "    parser.add_argument(\"--image_dir\", dest=\"image_dir\", default=\"C:/work/linedata(1028)/line/images/test2019/f4b5dac780890c2ca9f43c3fe4cc991a_d3d3LmVwc2lsb24uaW5zZWUuZnIJMTk1LjEwMS4yNTEuMTM2.xls-3-0.png\", type=str)\n",
    "    args = parser.parse_args()\n",
    "    return args\n",
    "\n",
    "def make_dirs(directories):\n",
    "    for directory in directories:\n",
    "        if not os.path.exists(directory):\n",
    "            os.makedirs(directory)\n",
    "def load_net(testiter, cfg_name, data_dir, cache_dir, result_dir, cuda_id=0):\n",
    "    cfg_file = os.path.join(system_configs.config_dir, cfg_name + \".json\")\n",
    "    with open(cfg_file, \"r\") as f:\n",
    "        configs = json.load(f)\n",
    "    configs[\"system\"][\"snapshot_name\"] = cfg_name\n",
    "    configs[\"system\"][\"data_dir\"] = data_dir\n",
    "    configs[\"system\"][\"cache_dir\"] = cache_dir\n",
    "    configs[\"system\"][\"result_dir\"] = result_dir\n",
    "    configs[\"system\"][\"tar_data_dir\"] = \"Cls\"\n",
    "    system_configs.update_config(configs[\"system\"])\n",
    "\n",
    "    train_split = system_configs.train_split\n",
    "    val_split = system_configs.val_split\n",
    "    test_split = system_configs.test_split\n",
    "\n",
    "    split = {\n",
    "        \"training\": train_split,\n",
    "        \"validation\": val_split,\n",
    "        \"testing\": test_split\n",
    "    }[\"validation\"]\n",
    "\n",
    "    result_dir = system_configs.result_dir\n",
    "    result_dir = os.path.join(result_dir, str(testiter), split)\n",
    "\n",
    "    make_dirs([result_dir])\n",
    "\n",
    "    test_iter = system_configs.max_iter if testiter is None else testiter\n",
    "    print(\"loading parameters at iteration: {}\".format(test_iter))\n",
    "    dataset = system_configs.dataset\n",
    "    db = datasets[dataset](configs[\"db\"], split)\n",
    "    print(\"building neural network...\")\n",
    "    nnet = NetworkFactory(db)\n",
    "    print(\"loading parameters...\")\n",
    "    nnet.load_params(test_iter)\n",
    "    if torch.cuda.is_available():\n",
    "        nnet.cuda(cuda_id)\n",
    "    nnet.eval_mode()\n",
    "    return db, nnet\n",
    "\n",
    "def Pre_load_nets():\n",
    "    methods = {}\n",
    "    db_cls, nnet_cls = load_net(50000, \"CornerNetCls\", \"data/clsdata(1031)\", \"data/clsdata(1031)/cache\",\n",
    "                                \"data/clsdata(1031)/result\")\n",
    "\n",
    "    from testfile.test_line_cls_pure_real import testing\n",
    "    path = 'testfile.test_%s' % \"CornerNetCls\"\n",
    "    testing_cls = importlib.import_module(path).testing\n",
    "    methods['Cls'] = [db_cls, nnet_cls, testing_cls]\n",
    "    db_bar, nnet_bar = load_net(50000, \"CornerNetPureBar\", \"data/bardata(1031)\", \"data/bardata(1031)/cache\",\n",
    "                                \"data/bardata(1031)/result\")\n",
    "    path = 'testfile.test_%s' % \"CornerNetPureBar\"\n",
    "    testing_bar = importlib.import_module(path).testing\n",
    "    methods['Bar'] = [db_bar, nnet_bar, testing_bar]\n",
    "    db_pie, nnet_pie = load_net(50000, \"CornerNetPurePie\", \"data/piedata(1008)\", \"data/piedata(1008)/cache\",\n",
    "                                \"data/piedata(1008)/result\")\n",
    "    path = 'testfile.test_%s' % \"CornerNetPurePie\"\n",
    "    testing_pie = importlib.import_module(path).testing\n",
    "    methods['Pie'] = [db_pie, nnet_pie, testing_pie]\n",
    "    db_line, nnet_line = load_net(50000, \"CornerNetLine\", \"data/linedata(1028)\", \"data/linedata(1028)/cache\",\n",
    "                                  \"data/linedata(1028)/result\")\n",
    "    path = 'testfile.test_%s' % \"CornerNetLine\"\n",
    "    testing_line = importlib.import_module(path).testing\n",
    "    methods['Line'] = [db_line, nnet_line, testing_line]\n",
    "    db_line_cls, nnet_line_cls = load_net(20000, \"CornerNetLineClsReal\", \"data/linedata(1028)\",\n",
    "                                          \"data/linedata(1028)/cache\",\n",
    "                                          \"data/linedata(1028)/result\")\n",
    "    path = 'testfile.test_%s' % \"CornerNetLineCls\"\n",
    "    testing_line_cls = importlib.import_module(path).testing\n",
    "    methods['LineCls'] = [db_line_cls, nnet_line_cls, testing_line_cls]\n",
    "    return methods\n",
    "methods = Pre_load_nets()\n",
    "\n",
    "def ocr_result(image_path):\n",
    "    ocr = PaddleOCR(use_angle_cls=True, lang='ch')\n",
    "    result = ocr.ocr(image_path, cls=True)\n",
    "    word_infos = []\n",
    "    word_dict = None\n",
    "    # visit every word\n",
    "    for word in result:\n",
    "        word_dict = dict()\n",
    "        word_dict[\"text\"] = word[1][0]\n",
    "        top = word[0][0][1]\n",
    "        left = word[0][0][0]\n",
    "        bottom = word[0][2][1]\n",
    "        right = word[0][2][0]\n",
    "        word_dict[\"boundingBox\"] = [left ,top ,0 ,0, right, bottom]\n",
    "        word_infos.append(word_dict)\n",
    "    return word_infos\n",
    "\n",
    "def check_intersection(box1, box2):\n",
    "    if (box1[2] - box1[0]) + ((box2[2] - box2[0])) > max(box2[2], box1[2]) - min(box2[0], box1[0]) \\\n",
    "            and (box1[3] - box1[1]) + ((box2[3] - box2[1])) > max(box2[3], box1[3]) - min(box2[1], box1[1]):\n",
    "        Xc1 = max(box1[0], box2[0])\n",
    "        Yc1 = max(box1[1], box2[1])\n",
    "        Xc2 = min(box1[2], box2[2])\n",
    "        Yc2 = min(box1[3], box2[3])\n",
    "        intersection_area = (Xc2-Xc1)*(Yc2-Yc1)\n",
    "        return intersection_area/((box2[3]-box2[1])*(box2[2]-box2[0]))\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "def try_math(image_path, cls_info):\n",
    "    title_list = [1, 2, 3]\n",
    "    title2string = {}\n",
    "    max_value = 1\n",
    "    min_value = 0\n",
    "    max_y = 0\n",
    "    min_y = 1\n",
    "    word_infos = ocr_result(image_path)\n",
    "    for id in title_list:\n",
    "        if id in cls_info.keys():\n",
    "            predicted_box = cls_info[id]\n",
    "            words = []\n",
    "            for word_info in word_infos:\n",
    "                word_bbox = [word_info[\"boundingBox\"][0], word_info[\"boundingBox\"][1], word_info[\"boundingBox\"][4], word_info[\"boundingBox\"][5]]\n",
    "                if check_intersection(predicted_box, word_bbox) > 0.5:\n",
    "                    words.append([word_info[\"text\"], word_bbox[0], word_bbox[1]])\n",
    "            words.sort(key=lambda x: x[1]+10*x[2])\n",
    "            word_string = \"\"\n",
    "            for word in words:\n",
    "                word_string = word_string + word[0] + ' '\n",
    "            title2string[id] = word_string\n",
    "    if 5 in cls_info.keys():\n",
    "        plot_area = cls_info[5]\n",
    "        y_max = plot_area[1]\n",
    "        y_min = plot_area[3]\n",
    "        x_board = plot_area[0]\n",
    "        dis_max = 10000000000000000\n",
    "        dis_min = 10000000000000000\n",
    "        for word_info in word_infos:\n",
    "            word_bbox = [word_info[\"boundingBox\"][0], word_info[\"boundingBox\"][1], word_info[\"boundingBox\"][4], word_info[\"boundingBox\"][5]]\n",
    "            word_text = word_info[\"text\"]\n",
    "            word_text = re.sub('[^-+0123456789.]', '',  word_text)\n",
    "            word_text_num = re.sub('[^0123456789]', '', word_text)\n",
    "            word_text_pure = re.sub('[^0123456789.]', '', word_text)\n",
    "            if len(word_text_num) > 0 and word_bbox[2] <= x_board+10:\n",
    "                dis2max = math.sqrt(math.pow((word_bbox[0]+word_bbox[2])/2-x_board, 2)+math.pow((word_bbox[1]+word_bbox[3])/2-y_max, 2))\n",
    "                dis2min = math.sqrt(math.pow((word_bbox[0] + word_bbox[2]) / 2 - x_board, 2) + math.pow(\n",
    "                    (word_bbox[1] + word_bbox[3]) / 2 - y_min, 2))\n",
    "                y_mid = (word_bbox[1]+word_bbox[3])/2\n",
    "                if dis2max <= dis_max:\n",
    "                    dis_max = dis2max\n",
    "                    max_y = y_mid\n",
    "                    max_value = float(word_text_pure)\n",
    "                    if word_text[0] == '-':\n",
    "                        max_value = -max_value\n",
    "                if dis2min <= dis_min:\n",
    "                    dis_min = dis2min\n",
    "                    min_y = y_mid\n",
    "                    min_value = float(word_text_pure)\n",
    "                    if word_text[0] == '-':\n",
    "                        min_value = -min_value\n",
    "        print(min_value)\n",
    "        print(max_value)\n",
    "        delta_min_max = max_value-min_value\n",
    "        delta_mark = min_y - max_y\n",
    "        delta_plot_y = y_min - y_max\n",
    "        delta = delta_min_max/delta_mark\n",
    "        if abs(min_y-y_min)/delta_plot_y > 0.1:\n",
    "            print(abs(min_y-y_min)/delta_plot_y)\n",
    "            print(\"Predict the lower bar\")\n",
    "            min_value = int(min_value + (min_y-y_min)*delta)\n",
    "\n",
    "    return title2string, round(min_value, 2), round(max_value, 2)\n",
    "\n",
    "\n",
    "def test(image_path, debug=False, suffix=None, min_value_official=None, max_value_official=None):\n",
    "    image_cls = Image.open(image_path)\n",
    "    image = cv2.imread(image_path)\n",
    "    with torch.no_grad():\n",
    "        results = methods['Cls'][2](image, methods['Cls'][0], methods['Cls'][1], debug=False)\n",
    "        info = results[0]\n",
    "        tls = results[1]\n",
    "        brs = results[2]\n",
    "        plot_area = []\n",
    "        image_painted, cls_info = GroupCls(image_cls, tls, brs)\n",
    "        title2string, min_value, max_value = try_math(image_path, cls_info)\n",
    "        if min_value_official is not None:\n",
    "            min_value = min_value_official\n",
    "            max_value = max_value_official\n",
    "        chartinfo = [info['data_type'], cls_info, title2string, min_value, max_value]\n",
    "        if info['data_type'] == 0:\n",
    "            print(\"Predicted as BarChart\")\n",
    "            results = methods['Bar'][2](image, methods['Bar'][0], methods['Bar'][1], debug=False)\n",
    "            tls = results[0]\n",
    "            brs = results[1]\n",
    "            if 5 in cls_info.keys():\n",
    "                plot_area = cls_info[5][0:4]\n",
    "            else:\n",
    "                plot_area = [0, 0, 600, 400]\n",
    "            image_painted, bar_data = GroupBar(image_painted, tls, brs, plot_area, min_value, max_value)\n",
    "\n",
    "            return plot_area, image_painted, bar_data, chartinfo\n",
    "        if info['data_type'] == 2:\n",
    "            print(\"Predicted as PieChart\")\n",
    "            results = methods['Pie'][2](image, methods['Pie'][0], methods['Pie'][1], debug=False)\n",
    "            cens = results[0]\n",
    "            keys = results[1]\n",
    "            image_painted, pie_data = GroupPie(image_painted, cens, keys)\n",
    "            return plot_area, image_painted, pie_data, chartinfo\n",
    "        if info['data_type'] == 1:\n",
    "            print(\"Predicted as LineChart\")\n",
    "            results = methods['Line'][2](image, methods['Line'][0], methods['Line'][1], debug=False, cuda_id=0)\n",
    "            keys = results[0]\n",
    "            hybrids = results[1]\n",
    "            if 5 in cls_info.keys():\n",
    "                plot_area = cls_info[5][0:4]\n",
    "            else:\n",
    "                plot_area = [0, 0, 600, 400]\n",
    "            print(min_value, max_value)\n",
    "            image_painted, quiry, keys, hybrids = GroupQuiry(image_painted, keys, hybrids, plot_area, min_value, max_value)\n",
    "            results = methods['LineCls'][2](image, methods['LineCls'][0], quiry, methods['LineCls'][1], debug=False, cuda_id=0)\n",
    "            line_data = GroupLine(image_painted, keys, hybrids, plot_area, results, min_value, max_value)\n",
    "            return plot_area, image_painted, line_data, chartinfo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[8.3252, 0.0000, 0.0000]], device='cuda:0')\n",
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:22,223] [   DEBUG] predict_system.py:70 - dt_boxes num : 14, elapse : 0.12107062339782715\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:22] root DEBUG: dt_boxes num : 14, elapse : 0.12107062339782715\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:22,283] [   DEBUG] predict_system.py:85 - cls num  : 14, elapse : 0.05489397048950195\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:22] root DEBUG: cls num  : 14, elapse : 0.05489397048950195\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:22,479] [   DEBUG] predict_system.py:89 - rec_res num  : 14, elapse : 0.19465994834899902\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:22] root DEBUG: rec_res num  : 14, elapse : 0.19465994834899902\n",
      "25.0\n",
      "10.0\n",
      "0.2471346385915146\n",
      "Predict the lower bar\n",
      "Predicted as BarChart\n"
     ]
    }
   ],
   "source": [
    "path = './image/testBar.png'\n",
    "plot_area, image_painted, data, chart_data = test(path)\n",
    "image_painted.save('static/target_bar.png')    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[91.11587524414062, 60.23095703125, 786.0899047851562, 435.8214111328125] [[0.5005035609621179, 0.33330921495028826, 0.3621473935733352, 0.6362361127677547, 0.24860532504646818], [0.2972738724865828, 0.44284845118542193, 0.361488600143589, 0.180443991670667, 0.62429005826438], [0.197783389240314, 0.22019877881083724, 0.2732735760782908, 0.17967550767232712, 0.1210846154671177]] [0, {0: [258.9734191894531, 479.2862548828125, 542.230712890625, 515.9313354492188, 0.6127422224260001], 1: [15.010784149169922, 196.69729614257812, 41.72893524169922, 300.73370361328125, 0.5305352067159409], 4: [39.157737731933594, 28.698196411132812, 785.898193359375, 468.64361572265625, 0.8069283988253129], 5: [91.11587524414062, 60.23095703125, 786.0899047851562, 435.8214111328125, 0.8665542898595067]}, {1: '水果消费总量 '}, 29, 10.0]\n"
     ]
    }
   ],
   "source": [
    "print(plot_area, data, chart_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[91.11587524414062, 60.23095703125, 786.0899047851562, 435.8214111328125]\n"
     ]
    }
   ],
   "source": [
    "print(plot_area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.5005035609621179, 0.33330921495028826, 0.3621473935733352, 0.6362361127677547, 0.24860532504646818], [0.2972738724865828, 0.44284845118542193, 0.361488600143589, 0.180443991670667, 0.62429005826438], [0.197783389240314, 0.22019877881083724, 0.2732735760782908, 0.17967550767232712, 0.1210846154671177]]\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, {0: [258.9734191894531, 479.2862548828125, 542.230712890625, 515.9313354492188, 0.6127422224260001], 1: [15.010784149169922, 196.69729614257812, 41.72893524169922, 300.73370361328125, 0.5305352067159409], 4: [39.157737731933594, 28.698196411132812, 785.898193359375, 468.64361572265625, 0.8069283988253129], 5: [91.11587524414062, 60.23095703125, 786.0899047851562, 435.8214111328125, 0.8665542898595067]}, {1: '水果消费总量 '}, 29, 10.0]\n"
     ]
    }
   ],
   "source": [
    "print(chart_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [258.9734191894531, 479.2862548828125, 542.230712890625, 515.9313354492188, 0.6127422224260001]\n",
      "1 [15.010784149169922, 196.69729614257812, 41.72893524169922, 300.73370361328125, 0.5305352067159409]\n",
      "4 [39.157737731933594, 28.698196411132812, 785.898193359375, 468.64361572265625, 0.8069283988253129]\n",
      "5 [91.11587524414062, 60.23095703125, 786.0899047851562, 435.8214111328125, 0.8665542898595067]\n"
     ]
    }
   ],
   "source": [
    "detect = chart_data[1] \n",
    "for key in detect.keys():\n",
    "    print(key, detect[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bar\n",
    "\n",
    "# 0 : 横轴说明检测框\n",
    "# 1 : 纵轴说明检测框\n",
    "# 4 : 统计图表（包含坐标轴）检测框\n",
    "# 5 : 内部图形检测框 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.4242, 7.7147, 0.1085]], device='cuda:0')\n",
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:23,595] [   DEBUG] predict_system.py:70 - dt_boxes num : 5, elapse : 0.07072305679321289\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:23] root DEBUG: dt_boxes num : 5, elapse : 0.07072305679321289\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:23,620] [   DEBUG] predict_system.py:85 - cls num  : 5, elapse : 0.02229475975036621\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:23] root DEBUG: cls num  : 5, elapse : 0.02229475975036621\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:23,651] [   DEBUG] predict_system.py:89 - rec_res num  : 5, elapse : 0.02975940704345703\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:23] root DEBUG: rec_res num  : 5, elapse : 0.02975940704345703\n",
      "0\n",
      "1\n",
      "1.1154011384501876\n",
      "Predict the lower bar\n",
      "Predicted as LineChart\n",
      "-349 1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "path = 'static/target.png'\n",
    "plot_area, image_painted, data, chart_data = test(path)\n",
    "image_painted.save('static/target_line.png')   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, {1: [5.6037139892578125, 155.54237365722656, 23.153709411621094, 230.9388427734375, 0.5418838428749925], 2: [249.81732177734375, 13.54449462890625, 386.1763916015625, 34.80779266357422, 0.6742527675671135], 3: [289.4371643066406, 377.6441955566406, 412.5398864746094, 395.5067443847656, 0.5715534173070195], 4: [18.459049224853516, 30.459068298339844, 662.77734375, 370.2933349609375, 0.9702154383094619], 5: [42.93842315673828, 37.11736297607422, 663.089111328125, 350.0896911621094, 0.9648130456914598]}, {1: '！ due ', 2: 'DRY Temperature ', 3: 'Days of the month '}, -349, 1]\n",
      "1 [5.6037139892578125, 155.54237365722656, 23.153709411621094, 230.9388427734375, 0.5418838428749925]\n",
      "2 [249.81732177734375, 13.54449462890625, 386.1763916015625, 34.80779266357422, 0.6742527675671135]\n",
      "3 [289.4371643066406, 377.6441955566406, 412.5398864746094, 395.5067443847656, 0.5715534173070195]\n",
      "4 [18.459049224853516, 30.459068298339844, 662.77734375, 370.2933349609375, 0.9702154383094619]\n",
      "5 [42.93842315673828, 37.11736297607422, 663.089111328125, 350.0896911621094, 0.9648130456914598]\n"
     ]
    }
   ],
   "source": [
    "print(chart_data)\n",
    "detect = chart_data[1] \n",
    "for key in detect.keys():\n",
    "    print(key, detect[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bar\n",
    "\n",
    "# 0 : 横轴说明检测框\n",
    "# 1 : 纵轴说明检测框\n",
    "# 4 : 统计图表（包含坐标轴）检测框\n",
    "# 5 : 内部图形检测框 \n",
    "\n",
    "# line\n",
    "# 1:\n",
    "# 2:\n",
    "# 3:\n",
    "# 4:\n",
    "# 5:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.6276, 0.0000, 6.8533]], device='cuda:0')\n",
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:24,887] [   DEBUG] predict_system.py:70 - dt_boxes num : 5, elapse : 0.11843276023864746\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:24] root DEBUG: dt_boxes num : 5, elapse : 0.11843276023864746\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:24,901] [   DEBUG] predict_system.py:85 - cls num  : 5, elapse : 0.010379552841186523\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:24] root DEBUG: cls num  : 5, elapse : 0.010379552841186523\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:24,949] [   DEBUG] predict_system.py:89 - rec_res num  : 5, elapse : 0.046222686767578125\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:24] root DEBUG: rec_res num  : 5, elapse : 0.046222686767578125\n",
      "0\n",
      "1\n",
      "1.4280862188337409\n",
      "Predict the lower bar\n",
      "Predicted as PieChart\n"
     ]
    }
   ],
   "source": [
    "path = 'image/testPie.jpg'\n",
    "plot_area, image_painted, data, chart_data = test(path)\n",
    "image_painted.save('static/target_pie.png')   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, {0: [950.4019775390625, 314.6844177246094, 1106.110595703125, 509.55560302734375, 0.4028762893852321], 4: [409.4755554199219, 198.01229858398438, 869.3472900390625, 658.2286987304688, 0.4253609426540074], 5: [409.4755554199219, 198.01229858398438, 869.3472900390625, 658.2286987304688, 0.9344542091530018]}, {}, -657, 1]\n",
      "0 [950.4019775390625, 314.6844177246094, 1106.110595703125, 509.55560302734375, 0.4028762893852321]\n",
      "4 [409.4755554199219, 198.01229858398438, 869.3472900390625, 658.2286987304688, 0.4253609426540074]\n",
      "5 [409.4755554199219, 198.01229858398438, 869.3472900390625, 658.2286987304688, 0.9344542091530018]\n"
     ]
    }
   ],
   "source": [
    "print(chart_data)\n",
    "detect = chart_data[1] \n",
    "for key in detect.keys():\n",
    "    print(key, detect[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[210.59677978729067, 82.30940293723195, 36.040749357907266, 31.0530679175701]\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先解决条形图的问题，再解决其他图的问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[9.5827, 0.0000, 0.6361]], device='cuda:0')\n",
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:26,119] [   DEBUG] predict_system.py:70 - dt_boxes num : 0, elapse : 0.06627464294433594\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:26] root DEBUG: dt_boxes num : 0, elapse : 0.06627464294433594\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:26,122] [   DEBUG] predict_system.py:85 - cls num  : 0, elapse : 0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:26] root DEBUG: cls num  : 0, elapse : 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:26,124] [   DEBUG] predict_system.py:89 - rec_res num  : 0, elapse : 3.814697265625e-06\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:26] root DEBUG: rec_res num  : 0, elapse : 3.814697265625e-06\n",
      "0\n",
      "1\n",
      "1.090440316645742\n",
      "Predict the lower bar\n",
      "Predicted as BarChart\n",
      "[0, {4: [50.055877685546875, 21.952829360961914, 452.7991943359375, 350.03240966796875, 0.9008718884857649], 5: [65.4809341430664, 28.527042388916016, 452.7172546386719, 332.89398193359375, 0.9682476410137094]}, {}, -331, 1]\n",
      "4 [50.055877685546875, 21.952829360961914, 452.7991943359375, 350.03240966796875, 0.9008718884857649]\n",
      "5 [65.4809341430664, 28.527042388916016, 452.7172546386719, 332.89398193359375, 0.9682476410137094]\n"
     ]
    }
   ],
   "source": [
    "path = 'image/line2.jpg'\n",
    "plot_area, image_painted, data, chart_data = test(path)\n",
    "image_painted.save('static/target_line2.png')   \n",
    "print(chart_data)\n",
    "detect = chart_data[1] \n",
    "for key in detect.keys():\n",
    "    print(key, detect[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ocr import ocr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:27,022] [   DEBUG] predict_system.py:70 - dt_boxes num : 0, elapse : 0.026614665985107422\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:27] root DEBUG: dt_boxes num : 0, elapse : 0.026614665985107422\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:27,024] [   DEBUG] predict_system.py:85 - cls num  : 0, elapse : 0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:27] root DEBUG: cls num  : 0, elapse : 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:27,026] [   DEBUG] predict_system.py:89 - rec_res num  : 0, elapse : 3.0994415283203125e-06\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:27] root DEBUG: rec_res num  : 0, elapse : 3.0994415283203125e-06\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(ocr_result(path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[7.6781, 0.0000, 0.0000]], device='cuda:0')\n",
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:27,988] [   DEBUG] predict_system.py:70 - dt_boxes num : 14, elapse : 0.11625432968139648\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:27] root DEBUG: dt_boxes num : 14, elapse : 0.11625432968139648\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:28,021] [   DEBUG] predict_system.py:85 - cls num  : 14, elapse : 0.02860879898071289\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:28] root DEBUG: cls num  : 14, elapse : 0.02860879898071289\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:28,088] [   DEBUG] predict_system.py:89 - rec_res num  : 14, elapse : 0.06464719772338867\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:28] root DEBUG: rec_res num  : 14, elapse : 0.06464719772338867\n",
      "0.0\n",
      "600.0\n",
      "Predicted as BarChart\n",
      "[0, {1: [14.823482513427734, 190.67999267578125, 42.23580551147461, 247.25775146484375, 0.5494805733624533], 3: [342.0087585449219, 470.701416015625, 384.5169677734375, 493.5316467285156, 0.40481340382897857], 4: [46.086753845214844, 0.0, 638.6184692382812, 460.97003173828125, 0.9296264522074748], 5: [86.10060119628906, 7.792552947998047, 638.6713256835938, 431.8367614746094, 0.9421247623817232]}, {1: '草 分分比 ', 3: '班级 '}, 0.0, 600.0]\n",
      "1 [14.823482513427734, 190.67999267578125, 42.23580551147461, 247.25775146484375, 0.5494805733624533]\n",
      "3 [342.0087585449219, 470.701416015625, 384.5169677734375, 493.5316467285156, 0.40481340382897857]\n",
      "4 [46.086753845214844, 0.0, 638.6184692382812, 460.97003173828125, 0.9296264522074748]\n",
      "5 [86.10060119628906, 7.792552947998047, 638.6713256835938, 431.8367614746094, 0.9421247623817232]\n"
     ]
    }
   ],
   "source": [
    "path = 'image/bar3.jpg'\n",
    "plot_area, image_painted, data, chart_data = test(path)\n",
    "image_painted.save('static/target_bar3.png')   \n",
    "print(chart_data)\n",
    "detect = chart_data[1] \n",
    "for key in detect.keys():\n",
    "    print(key, detect[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:29,085] [   DEBUG] predict_system.py:70 - dt_boxes num : 14, elapse : 0.04926156997680664\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:29] root DEBUG: dt_boxes num : 14, elapse : 0.04926156997680664\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:29,119] [   DEBUG] predict_system.py:85 - cls num  : 14, elapse : 0.02989792823791504\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:29] root DEBUG: cls num  : 14, elapse : 0.02989792823791504\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:29,155] [   DEBUG] predict_system.py:89 - rec_res num  : 14, elapse : 0.033492088317871094\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:29] root DEBUG: rec_res num  : 14, elapse : 0.033492088317871094\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'text': '性别', 'boundingBox': [646.0, 15.0, 0, 0, 681.0, 34.0]},\n",
       " {'text': '男', 'boundingBox': [662.0, 40.0, 0, 0, 677.0, 54.0]},\n",
       " {'text': '600%', 'boundingBox': [48.0, 82.0, 0, 0, 83.0, 96.0]},\n",
       " {'text': '草', 'boundingBox': [20.0, 195.0, 0, 0, 39.0, 213.0]},\n",
       " {'text': '40.0%', 'boundingBox': [42.0, 196.0, 0, 0, 81.0, 210.0]},\n",
       " {'text': '分分比', 'boundingBox': [19.0, 208.0, 0, 0, 39.0, 244.0]},\n",
       " {'text': '20.0%', 'boundingBox': [49.0, 312.0, 0, 0, 82.0, 326.0]},\n",
       " {'text': '0%', 'boundingBox': [64.0, 428.0, 0, 0, 81.0, 440.0]},\n",
       " {'text': '二班', 'boundingBox': [162.0, 440.0, 0, 0, 194.0, 459.0]},\n",
       " {'text': '三班', 'boundingBox': [285.0, 440.0, 0, 0, 320.0, 458.0]},\n",
       " {'text': '四班', 'boundingBox': [408.0, 440.0, 0, 0, 444.0, 458.0]},\n",
       " {'text': '班', 'boundingBox': [547.0, 443.0, 0, 0, 565.0, 456.0]},\n",
       " {'text': '班级', 'boundingBox': [342.0, 470.0, 0, 0, 383.0, 492.0]}]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ocr_result(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[8.0693, 0.0000, 0.0000]], device='cuda:0')\n",
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:30,236] [   DEBUG] predict_system.py:70 - dt_boxes num : 20, elapse : 0.13698220252990723\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:30] root DEBUG: dt_boxes num : 20, elapse : 0.13698220252990723\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:30,271] [   DEBUG] predict_system.py:85 - cls num  : 20, elapse : 0.030659198760986328\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:30] root DEBUG: cls num  : 20, elapse : 0.030659198760986328\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:30,383] [   DEBUG] predict_system.py:89 - rec_res num  : 20, elapse : 0.10922527313232422\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:30] root DEBUG: rec_res num  : 20, elapse : 0.10922527313232422\n",
      "0.0\n",
      "110.0\n",
      "Predicted as BarChart\n",
      "[0, {4: [123.35517883300781, 158.2377166748047, 622.4466552734375, 671.0560913085938, 0.6582223303333283], 5: [189.44850158691406, 169.50241088867188, 622.4642333984375, 622.185791015625, 0.8453660373286227]}, {}, 0.0, 110.0]\n",
      "4 [123.35517883300781, 158.2377166748047, 622.4466552734375, 671.0560913085938, 0.6582223303333283]\n",
      "5 [189.44850158691406, 169.50241088867188, 622.4642333984375, 622.185791015625, 0.8453660373286227]\n"
     ]
    }
   ],
   "source": [
    "path = 'image/bar4.jpeg'\n",
    "plot_area, image_painted, data, chart_data = test(path)\n",
    "image_painted.save('static/target_bar4.png')   \n",
    "print(chart_data)\n",
    "detect = chart_data[1] \n",
    "for key in detect.keys():\n",
    "    print(key, detect[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:31,593] [   DEBUG] predict_system.py:70 - dt_boxes num : 20, elapse : 0.06291675567626953\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:31] root DEBUG: dt_boxes num : 20, elapse : 0.06291675567626953\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:31,633] [   DEBUG] predict_system.py:85 - cls num  : 20, elapse : 0.03323793411254883\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:31] root DEBUG: cls num  : 20, elapse : 0.03323793411254883\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-26 21:48:31,684] [   DEBUG] predict_system.py:89 - rec_res num  : 20, elapse : 0.04886960983276367\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/26 21:48:31] root DEBUG: rec_res num  : 20, elapse : 0.04886960983276367\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'text': '某电器专卖店计划销售空调统计图', 'boundingBox': [150.0, 71.0, 0, 0, 702.0, 101.0]},\n",
       " {'text': '销售量（台）', 'boundingBox': [187.0, 128.0, 0, 0, 388.0, 160.0]},\n",
       " {'text': '110', 'boundingBox': [129.0, 159.0, 0, 0, 186.0, 189.0]},\n",
       " {'text': '100', 'boundingBox': [128.0, 199.0, 0, 0, 187.0, 228.0]},\n",
       " {'text': '90', 'boundingBox': [494.0, 215.0, 0, 0, 536.0, 244.0]},\n",
       " {'text': '90', 'boundingBox': [147.0, 239.0, 0, 0, 187.0, 270.0]},\n",
       " {'text': '80', 'boundingBox': [374.0, 258.0, 0, 0, 417.0, 290.0]},\n",
       " {'text': '80', 'boundingBox': [147.0, 280.0, 0, 0, 186.0, 310.0]},\n",
       " {'text': '70', 'boundingBox': [148.0, 320.0, 0, 0, 186.0, 351.0]},\n",
       " {'text': '60', 'boundingBox': [256.0, 341.0, 0, 0, 296.0, 370.0]},\n",
       " {'text': '60', 'boundingBox': [148.0, 360.0, 0, 0, 186.0, 390.0]},\n",
       " {'text': '50', 'boundingBox': [148.0, 401.0, 0, 0, 186.0, 431.0]},\n",
       " {'text': '40', 'boundingBox': [148.0, 441.0, 0, 0, 186.0, 470.0]},\n",
       " {'text': '30', 'boundingBox': [146.0, 479.0, 0, 0, 187.0, 512.0]},\n",
       " {'text': '20', 'boundingBox': [147.0, 521.0, 0, 0, 186.0, 551.0]},\n",
       " {'text': '10', 'boundingBox': [147.0, 561.0, 0, 0, 186.0, 592.0]},\n",
       " {'text': '0l', 'boundingBox': [167.0, 602.0, 0, 0, 190.0, 629.0]},\n",
       " {'text': '4月', 'boundingBox': [255.0, 631.0, 0, 0, 310.0, 668.0]},\n",
       " {'text': '5月', 'boundingBox': [365.0, 631.0, 0, 0, 423.0, 668.0]},\n",
       " {'text': '6月', 'boundingBox': [487.0, 630.0, 0, 0, 544.0, 669.0]}]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ocr_result(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_number(s):\n",
    "    if s.count(\".\") == 1 and s[-1] != \"%\":  # 小数的判断\n",
    "        if s[0] == \"-\":\n",
    "            s = s[1:]\n",
    "        if s[0] == \".\":\n",
    "            return False\n",
    "        s = s.replace(\".\", \"\")\n",
    "        for i in s:\n",
    "            if i not in \"0123456789\":\n",
    "                return False\n",
    "        else:  # 这个else与for对应的\n",
    "            return True\n",
    "    elif s.count(\".\") == 0 and s[-1] != \"%\":  # 整数的判断\n",
    "        if s[0] == \"-\":\n",
    "            s = s[1:]\n",
    "        for i in s:\n",
    "            if i not in \"0123456789\":\n",
    "                return False\n",
    "        else:\n",
    "            return True\n",
    "    elif s[-1] == \"%\":  # 百分数判断\n",
    "        return True\n",
    "\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "# 对横坐标说明进行聚类\n",
    "def getXAxisList(word_infos, bbox):\n",
    "    tl_x = bbox[0]\n",
    "    tl_y = bbox[1]\n",
    "    br_x = bbox[2]\n",
    "    br_y = bbox[3]\n",
    "    Xs = []\n",
    "    \n",
    "    height = br_y - tl_y\n",
    "    threshold = height / 10\n",
    "    # 聚类方法\n",
    "    # 1. 粗略聚类\n",
    "    for word_info in word_infos:\n",
    "        wbox = word_info[\"boundingBox\"]\n",
    "        wcen_x = wbox[0] + wbox[2]\n",
    "        wcen_y = wbox[1] + wbox[3]\n",
    "        if wcen_x > tl_x and wcen_x < br_x and \\\n",
    "           br_y < wcen_y and br_y + threshold > wcen_y:\n",
    "               Xs.append(word_info)    \n",
    "    # 2. 偏差剔除, 计算左上角，y坐标的平均值\n",
    "    threshold = height / 20\n",
    "    if len(Xs) == 0:\n",
    "        return []\n",
    "    mean = 0\n",
    "    for x in Xs:\n",
    "        mean = x[\"boundingBox\"][1]\n",
    "    mean = mean / len(Xs)\n",
    "    filtedXs = []\n",
    "    # 将偏差较大的剔除\n",
    "    for x in Xs:\n",
    "        if abs(x[\"boundingBox\"][1] - mean) < threshold:\n",
    "            filtedXs.append(x)\n",
    "    # 按照左上角横坐标进行排序\n",
    "    filtedXs.sort(lambda a:a[\"boundingBox\"][0])\n",
    "    return filtedXs\n",
    "\n",
    "def getDistance(ax, ay, bx, by):\n",
    "    return abs(ax - bx) + abs(bx - by)\n",
    "\n",
    "# 获取y轴的说明，首先y轴说明不能是数字，其次位置在bbox的左上角，\n",
    "# 即中心点位置离角点不能太远\n",
    "def getYAxisInstruction(word_infos, bbox):\n",
    "    tl_x = bbox[0]\n",
    "    tl_y = bbox[1]\n",
    "    br_x = bbox[2]\n",
    "    br_y = bbox[3]\n",
    "    \n",
    "    height = br_y - tl_y\n",
    "    threshold_h = height / 10\n",
    "    width = br_x - tl_x\n",
    "    threshold_w = width / 10\n",
    "    \n",
    "    Yi = None\n",
    "    Score = 9999999\n",
    "    for word_info in word_infos:\n",
    "        if is_number(word_info[\"text\"]):\n",
    "            continue\n",
    "        wbox = word_info[\"boundingBox\"]\n",
    "        wcen_x = wbox[0] + wbox[2]\n",
    "        wcen_y = wbox[1] + wbox[3]\n",
    "        score = getDistance(wcen_x, wcen_y, br_x, br_y)\n",
    "        if score > Score:\n",
    "            continue\n",
    "        if wcen_x > br_x - threshold_w and wcen_x < br_x + threshold_w and \\\n",
    "            br_y - threshold_h < wcen_y and br_y + threshold_h > wcen_y:\n",
    "            Yi = word_info\n",
    "            Score = score\n",
    "\n",
    "    return Yi    \n",
    "    \n",
    "    \n",
    "\n",
    "def GetTableFromPicture(picturePath):\n",
    "    # OCR提取出来的文字信息还需要使用\n",
    "    word_infos = ocr_result(picturePath)\n",
    "    # 该接口函数有所调整，源文件是test_pipeline.py，主要是删除了\n",
    "    # image_painted没用\n",
    "    # data 就是数据表格，各个检测出来的bar的长度或者line高度，或者角度\n",
    "    # chart_data包含图片种类，检测出来的检测框，以及统计图表说明，检测出来图表value轴的最大值和最小\n",
    "    # 具体格式如下：\n",
    "    # [ \n",
    "    #    0, \n",
    "    #    {\n",
    "    #       1: [14.823482513427734, 190.67999267578125, 42.23580551147461, 247.25775146484375, 0.5494805733624533], \n",
    "    #       3: [342.0087585449219, 470.701416015625, 384.5169677734375, 493.5316467285156, 0.40481340382897857], \n",
    "    #       4: [46.086753845214844, 0.0, 638.6184692382812, 460.97003173828125, 0.9296264522074748], \n",
    "    #       5: [86.10060119628906, 7.792552947998047, 638.6713256835938, 431.8367614746094, 0.9421247623817232]\n",
    "    #    }, \n",
    "    #    {1: '草 分分比 ', 3: '班级 '}, \n",
    "    #    0.0, \n",
    "    #    600.0\n",
    "    # ]\n",
    "    # 目前要做的就是添加横轴纵轴说明\n",
    "    plot_area, image_painted, data, chart_data = test(picturePath, word_infos)\n",
    "    cls = chart_data[0]\n",
    "    if chart_data[0]==0:\n",
    "        min_value = chart_data[3]\n",
    "        max_value = chart_data[4]\n",
    "        for k in range(len(data)):\n",
    "            for j in range(len(data[k])):\n",
    "                data[k][j] = round((max_value - min_value) * data[k][j] + min_value, 2)\n",
    "        # 采用启发式规则，横轴通过ocr读取的图像，对文字框按照横轴进行聚类\n",
    "        Xs = []\n",
    "        Yi = None\n",
    "        if 4 in chart_data.keys():\n",
    "            Xs = getXAxisList(word_infos, chart_data[4])\n",
    "            Yi = getYAxisInstruction(word_infos,chart_data[4])\n",
    "        if len(Xs == 0):\n",
    "            pass\n",
    "        return Xs, Yi, data, chart_data\n",
    "\n",
    "    if chart_data[0] == 1:\n",
    "        min_value = chart_data[3]\n",
    "        max_value = chart_data[4]\n",
    "        for k in range(len(data)):\n",
    "            for j in range(len(data[k])):\n",
    "                data[k][j] = round((max_value - min_value) * data[k][j] + min_value, 2)\n",
    "        \n",
    "        # 采用启发式规则，横轴通过ocr读取的图像，对文字框按照横轴进行聚类\n",
    "        Xs = []\n",
    "        Yi = None\n",
    "        if 4 in chart_data.keys():\n",
    "            Xs = getXAxisList(word_infos, chart_data[4])\n",
    "            Yi = getYAxisInstruction(word_infos,chart_data[4])\n",
    "        if len(Xs == 0):\n",
    "            pass\n",
    "        return Xs, Yi, data, chart_data\n",
    "    \n",
    "    # 饼图不同于折线和条形，需要单独考虑\n",
    "    if chart_data[0]==2:\n",
    "        for k in range(len(data)):\n",
    "            data[k] /= 360\n",
    "        data = [round(x, 2) for x in data]\n",
    "        min_value = 0\n",
    "        max_value = 1\n",
    "        return Xs, Yi, data, chart_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from test_api import test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "barPath = \"./image/testBar.png\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_number(s):\n",
    "    if s.count(\".\") == 1 and s[-1] != \"%\":  # 小数的判断\n",
    "        if s[0] == \"-\":\n",
    "            s = s[1:] \n",
    "        if s[0] == \".\":\n",
    "            return False\n",
    "        s = s.replace(\".\", \"\")\n",
    "        for i in s:\n",
    "            if i not in \"0123456789\":\n",
    "                return False\n",
    "        else:  # 这个else与for对应的\n",
    "            return True\n",
    "    elif s.count(\".\") == 0 and s[-1] != \"%\":  # 整数的判断\n",
    "        if s[0] == \"-\":\n",
    "            s = s[1:]\n",
    "        for i in s:\n",
    "            if i not in \"0123456789\":\n",
    "                return False\n",
    "        else:\n",
    "            return True\n",
    "    elif s[-1] == \"%\":  # 百分数判断\n",
    "        return True\n",
    "\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "def getXAxisList(word_infos, bbox):\n",
    "    tl_x = bbox[0]\n",
    "    tl_y = bbox[1]\n",
    "    br_x = bbox[2]\n",
    "    br_y = bbox[3]\n",
    "    Xs = []\n",
    "    \n",
    "    height = br_y - tl_y\n",
    "    threshold = height / 10\n",
    "    # 聚类方法\n",
    "    # 1. 粗略聚类\n",
    "    for word_info in word_infos:\n",
    "        wbox = word_info[\"boundingBox\"]\n",
    "        wcen_x = (wbox[0] + wbox[4]) / 2\n",
    "        wcen_y = (wbox[1] + wbox[5]) / 2\n",
    "        if wcen_x > tl_x and wcen_x < br_x and \\\n",
    "           br_y < wcen_y and br_y + threshold > wcen_y:\n",
    "               Xs.append(word_info)    \n",
    "    # 2. 偏差剔除, 计算左上角，y坐标的平均值\n",
    "    threshold = height / 20\n",
    "    if len(Xs) == 0:\n",
    "        return []\n",
    "    mean = 0\n",
    "    for x in Xs:\n",
    "        mean = x[\"boundingBox\"][1]\n",
    "    mean = mean / len(Xs)\n",
    "    filtedXs = []\n",
    "    # 将偏差较大的剔除\n",
    "    for x in Xs:\n",
    "        if abs(x[\"boundingBox\"][1] - mean) < threshold:\n",
    "            filtedXs.append(x)\n",
    "    # 按照左上角横坐标进行排序\n",
    "    filtedXs.sort(key = lambda a:a[\"boundingBox\"][0])\n",
    "    return filtedXs\n",
    "\n",
    "def getDistance(ax, ay, bx, by):\n",
    "    return abs(ax - bx) + abs(bx - by)\n",
    "\n",
    "# 获取y轴的说明，首先y轴说明不能是数字，其次位置在bbox的左上角，\n",
    "# 即中心点位置离角点不能太远\n",
    "def getYAxisInstruction(word_infos, bbox):\n",
    "    tl_x = bbox[0]\n",
    "    tl_y = bbox[1]\n",
    "    br_x = bbox[2]\n",
    "    br_y = bbox[3]\n",
    "    \n",
    "    height = br_y - tl_y\n",
    "    threshold_h = height / 10\n",
    "    width = br_x - tl_x\n",
    "    threshold_w = width / 10\n",
    "    \n",
    "    Yi = None\n",
    "    Score = 9999999\n",
    "    for word_info in word_infos:\n",
    "        if is_number(word_info[\"text\"]):\n",
    "            continue\n",
    "        wbox = word_info[\"boundingBox\"]\n",
    "        wcen_x = (wbox[0] + wbox[4]) / 2\n",
    "        wcen_y = (wbox[1] + wbox[5]) / 2\n",
    "        score = getDistance(wcen_x, wcen_y, br_x, br_y)\n",
    "        if score > Score:\n",
    "            continue\n",
    "        if wcen_x > br_x - threshold_w and wcen_x < br_x + threshold_w and \\\n",
    "            br_y - threshold_h < wcen_y and br_y + threshold_h > wcen_y:\n",
    "            Yi = word_info\n",
    "            Score = score\n",
    "\n",
    "    return Yi    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ocr import ocr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "picturePath = barPath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-27 10:52:16,489] [   DEBUG] predict_system.py:70 - dt_boxes num : 14, elapse : 0.0480802059173584\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/27 10:52:16] root DEBUG: dt_boxes num : 14, elapse : 0.0480802059173584\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-27 10:52:16,514] [   DEBUG] predict_system.py:85 - cls num  : 14, elapse : 0.022513151168823242\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/27 10:52:16] root DEBUG: cls num  : 14, elapse : 0.022513151168823242\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/ocr/lib/python3.8/site-packages/numpy/core/fromnumeric.py:3432: RuntimeWarning: Mean of empty slice.\n",
      "  return _methods._mean(a, axis=axis, dtype=dtype,\n",
      "[2024-03-27 10:52:16,543] [   DEBUG] predict_system.py:89 - rec_res num  : 14, elapse : 0.02724933624267578\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/27 10:52:16] root DEBUG: rec_res num  : 14, elapse : 0.02724933624267578\n"
     ]
    }
   ],
   "source": [
    "word_infos = ocr_result(picturePath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'text': '1O0', 'boundingBox': [42.0, 55.0, 0, 0, 71.0, 71.0]}, {'text': '75', 'boundingBox': [52.0, 148.0, 0, 0, 71.0, 165.0]}, {'text': '水果消费总量', 'boundingBox': [15.0, 196.0, 0, 0, 37.0, 300.0]}, {'text': '50', 'boundingBox': [50.0, 241.0, 0, 0, 71.0, 257.0]}, {'text': '25', 'boundingBox': [52.0, 334.0, 0, 0, 71.0, 352.0]}, {'text': '橘子', 'boundingBox': [283.0, 447.0, 0, 0, 316.0, 465.0]}, {'text': '梨', 'boundingBox': [429.0, 447.0, 0, 0, 447.0, 464.0]}, {'text': '苹果', 'boundingBox': [144.0, 448.0, 0, 0, 175.0, 463.0]}, {'text': '葡萄', 'boundingBox': [561.0, 447.0, 0, 0, 596.0, 464.0]}, {'text': '香蕉', 'boundingBox': [701.0, 449.0, 0, 0, 731.0, 464.0]}, {'text': '小张', 'boundingBox': [308.0, 489.0, 0, 0, 347.0, 507.0]}, {'text': '〇小彭', 'boundingBox': [371.0, 489.0, 0, 0, 432.0, 506.0]}, {'text': '小潘', 'boundingBox': [479.0, 488.0, 0, 0, 518.0, 507.0]}]\n"
     ]
    }
   ],
   "source": [
    "print(word_infos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[8.3252, 0.0000, 0.0000]], device='cuda:0')\n",
      "25.0\n",
      "10.0\n",
      "0.2471346385915146\n",
      "Predict the lower bar\n",
      "Predicted as BarChart\n"
     ]
    }
   ],
   "source": [
    "plot_area, image_painted, data, chart_data = test(picturePath, word_infos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "cls = chart_data[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "min_value = chart_data[3]\n",
    "max_value = chart_data[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29\n"
     ]
    }
   ],
   "source": [
    "print(min_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "for k in range(len(data)):\n",
    "    for j in range(len(data[k])):\n",
    "        data[k][j] = round((max_value - min_value) * data[k][j] + min_value, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    }
   ],
   "source": [
    "print(max_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1O0 [42.0, 55.0, 0, 0, 71.0, 71.0]\n",
      "75 [52.0, 148.0, 0, 0, 71.0, 165.0]\n",
      "水果消费总量 [15.0, 196.0, 0, 0, 37.0, 300.0]\n",
      "50 [50.0, 241.0, 0, 0, 71.0, 257.0]\n",
      "25 [52.0, 334.0, 0, 0, 71.0, 352.0]\n",
      "橘子 [283.0, 447.0, 0, 0, 316.0, 465.0]\n",
      "梨 [429.0, 447.0, 0, 0, 447.0, 464.0]\n",
      "苹果 [144.0, 448.0, 0, 0, 175.0, 463.0]\n",
      "葡萄 [561.0, 447.0, 0, 0, 596.0, 464.0]\n",
      "香蕉 [701.0, 449.0, 0, 0, 731.0, 464.0]\n",
      "小张 [308.0, 489.0, 0, 0, 347.0, 507.0]\n",
      "〇小彭 [371.0, 489.0, 0, 0, 432.0, 506.0]\n",
      "小潘 [479.0, 488.0, 0, 0, 518.0, 507.0]\n"
     ]
    }
   ],
   "source": [
    "for word_info in word_infos:\n",
    "    print(word_info[\"text\"], word_info[\"boundingBox\"]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[19.49, 22.67, 22.12, 16.91, 24.28], [23.35, 20.59, 22.13, 25.57, 17.14], [25.24, 24.82, 23.81, 25.59, 26.7]]\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "Xs = []\n",
    "Yi = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "if 4 in chart_data[1].keys():\n",
    "    Xs = getXAxisList(word_infos, chart_data[1][4])\n",
    "    Yi = getYAxisInstruction(word_infos,chart_data[1][4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(Xs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[39.157737731933594, 28.698196411132812, 785.898193359375, 468.64361572265625, 0.8069283988253129]\n"
     ]
    }
   ],
   "source": [
    "print(chart_data[1][4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "bbox = chart_data[1][4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "tl_x = bbox[0]\n",
    "tl_y = bbox[1]\n",
    "br_x = bbox[2]\n",
    "br_y = bbox[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "height = br_y - tl_y\n",
    "threshold = height / 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "43.99454193115234\n"
     ]
    }
   ],
   "source": [
    "print(threshold)\n",
    "Xs = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1O0\n",
      "56.5\n",
      "63.0\n",
      "\n",
      "75\n",
      "61.5\n",
      "156.5\n",
      "\n",
      "水果消费总量\n",
      "26.0\n",
      "248.0\n",
      "\n",
      "50\n",
      "60.5\n",
      "249.0\n",
      "\n",
      "25\n",
      "61.5\n",
      "343.0\n",
      "\n",
      "橘子\n",
      "299.5\n",
      "456.0\n",
      "\n",
      "{'text': '橘子', 'boundingBox': [283.0, 447.0, 0, 0, 316.0, 465.0]}\n",
      "梨\n",
      "438.0\n",
      "455.5\n",
      "\n",
      "{'text': '梨', 'boundingBox': [429.0, 447.0, 0, 0, 447.0, 464.0]}\n",
      "苹果\n",
      "159.5\n",
      "455.5\n",
      "\n",
      "{'text': '苹果', 'boundingBox': [144.0, 448.0, 0, 0, 175.0, 463.0]}\n",
      "葡萄\n",
      "578.5\n",
      "455.5\n",
      "\n",
      "{'text': '葡萄', 'boundingBox': [561.0, 447.0, 0, 0, 596.0, 464.0]}\n",
      "香蕉\n",
      "716.0\n",
      "456.5\n",
      "\n",
      "{'text': '香蕉', 'boundingBox': [701.0, 449.0, 0, 0, 731.0, 464.0]}\n",
      "小张\n",
      "327.5\n",
      "498.0\n",
      "\n",
      "〇小彭\n",
      "401.5\n",
      "497.5\n",
      "\n",
      "小潘\n",
      "498.5\n",
      "497.5\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for word_info in word_infos:\n",
    "    wbox = word_info[\"boundingBox\"]\n",
    "    wcen_x = (wbox[0] + wbox[4]) / 2\n",
    "    wcen_y = (wbox[1] + wbox[5]) / 2\n",
    "    print(word_info[\"text\"])\n",
    "    print(wcen_x)\n",
    "    print(wcen_y)\n",
    "    print(\"\")\n",
    "    if wcen_x > tl_x and wcen_x < br_x and \\\n",
    "        br_y > wcen_y and br_y - threshold < wcen_y:\n",
    "        print(word_info)\n",
    "        Xs.append(word_info) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "39.157737731933594 28.698196411132812 785.898193359375 468.64361572265625\n"
     ]
    }
   ],
   "source": [
    "print(tl_x, tl_y, br_x, br_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'text': '橘子', 'boundingBox': [283.0, 447.0, 0, 0, 316.0, 465.0]}, {'text': '梨', 'boundingBox': [429.0, 447.0, 0, 0, 447.0, 464.0]}, {'text': '苹果', 'boundingBox': [144.0, 448.0, 0, 0, 175.0, 463.0]}, {'text': '葡萄', 'boundingBox': [561.0, 447.0, 0, 0, 596.0, 464.0]}, {'text': '香蕉', 'boundingBox': [701.0, 449.0, 0, 0, 731.0, 464.0]}]\n"
     ]
    }
   ],
   "source": [
    "print(Xs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'text': '橘子', 'boundingBox': [283.0, 447.0, 0, 0, 316.0, 465.0]}\n",
      "{'text': '梨', 'boundingBox': [429.0, 447.0, 0, 0, 447.0, 464.0]}\n",
      "{'text': '苹果', 'boundingBox': [144.0, 448.0, 0, 0, 175.0, 463.0]}\n",
      "{'text': '葡萄', 'boundingBox': [561.0, 447.0, 0, 0, 596.0, 464.0]}\n",
      "{'text': '香蕉', 'boundingBox': [701.0, 449.0, 0, 0, 731.0, 464.0]}\n"
     ]
    }
   ],
   "source": [
    "for i in Xs:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "Xs.sort(key = lambda a:a[\"boundingBox\"][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'text': '苹果', 'boundingBox': [144.0, 448.0, 0, 0, 175.0, 463.0]},\n",
       " {'text': '橘子', 'boundingBox': [283.0, 447.0, 0, 0, 316.0, 465.0]},\n",
       " {'text': '梨', 'boundingBox': [429.0, 447.0, 0, 0, 447.0, 464.0]},\n",
       " {'text': '葡萄', 'boundingBox': [561.0, 447.0, 0, 0, 596.0, 464.0]},\n",
       " {'text': '香蕉', 'boundingBox': [701.0, 449.0, 0, 0, 731.0, 464.0]}]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<frozen importlib._bootstrap>:219: RuntimeWarning: compiletime version 3.6 of module 'pycocotools._mask' does not match runtime version 3.8\n",
      "<frozen importlib._bootstrap>:219: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 864 from C header, got 880 from PyObject\n",
      "WARNING: OMP_NUM_THREADS set to 14, not 1. The computation speed will not be optimized if you use data parallel. It will fail if this PaddlePaddle binary is compiled with OpenBlas since OpenBlas does not support multi-threads.\n",
      "PLEASE USE OMP_NUM_THREADS WISELY.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/clsdata(1031)/cache/cls_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/clsdata(1031)/Cls/annotations/instancesCls(1031)_val2019.json\n",
      "Done (t=0.03s)\n",
      "creating index...\n",
      "index created!\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/clsdata(1031)/Cls/annotations/instancesCls(1031)_val2019.json\n",
      "Done (t=0.11s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetCls\n",
      "use kp\n",
      "total parameters: 199649452\n",
      "loading parameters...\n",
      "loading model from data/clsdata(1031)/cache/nnet/CornerNetCls/CornerNetCls_50000.pkl\n",
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/bardata(1031)/cache/chart_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/bardata(1031)/bar/annotations/instancesBar(1031)_val2019.json\n",
      "Done (t=0.57s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetPureBar\n",
      "use kp pure\n",
      "total parameters: 198592652\n",
      "loading parameters...\n",
      "loading model from data/bardata(1031)/cache/nnet/CornerNetPureBar/CornerNetPureBar_50000.pkl\n",
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/piedata(1008)/cache/pie_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/piedata(1008)/pie/annotations/instancesPie(1008)_val2019.json\n",
      "Done (t=0.03s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetPurePie\n",
      "use kp pure pie\n",
      "total parameters: 198592652\n",
      "loading parameters...\n",
      "loading model from data/piedata(1008)/cache/nnet/CornerNetPurePie/CornerNetPurePie_50000.pkl\n",
      "loading parameters at iteration: 50000\n",
      "loading from cache file: data/linedata(1028)/cache/line_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/linedata(1028)/line/annotations/instancesLine(1023)_val2019.json\n",
      "Done (t=0.05s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetLine\n",
      "use kp\n",
      "total parameters: 198592138\n",
      "loading parameters...\n",
      "loading model from data/linedata(1028)/cache/nnet/CornerNetLine/CornerNetLine_50000.pkl\n",
      "loading parameters at iteration: 20000\n",
      "loading from cache file: data/linedata(1028)/cache/line_real_val2019.pkl\n",
      "loading annotations into memory...\n",
      "/root/autodl-tmp/DeepRule/data/linedata(1028)/line/annotations/instancesLineClsReal(1119)_val2019.json\n",
      "Done (t=0.03s)\n",
      "creating index...\n",
      "index created!\n",
      "building neural network...\n",
      "module_file: models.CornerNetLineClsReal\n",
      "use kp pure\n",
      "total parameters: 188750476\n",
      "loading parameters...\n",
      "loading model from data/linedata(1028)/cache/nnet/CornerNetLineClsReal/CornerNetLineClsReal_20000.pkl\n"
     ]
    }
   ],
   "source": [
    "from test_api import GetTableFromPicture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'bar4.jpeg'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m barPath \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mbar4.jpeg\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m----> 2\u001b[0m res \u001b[39m=\u001b[39m GetTableFromPicture(barPath)\n",
      "File \u001b[0;32m~/autodl-tmp/DeepRule/test_api.py:382\u001b[0m, in \u001b[0;36mGetTableFromPicture\u001b[0;34m(picturePath)\u001b[0m\n\u001b[1;32m    380\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mGetTableFromPicture\u001b[39m(picturePath):\n\u001b[1;32m    381\u001b[0m     \u001b[39m# OCR提取出来的文字信息还需要使用\u001b[39;00m\n\u001b[0;32m--> 382\u001b[0m     word_infos \u001b[39m=\u001b[39m ocr_result(picturePath)\n\u001b[1;32m    383\u001b[0m     \u001b[39m# 该接口函数有所调整，源文件是test_pipeline.py，主要是删除了\u001b[39;00m\n\u001b[1;32m    384\u001b[0m     \u001b[39m# image_painted没用\u001b[39;00m\n\u001b[1;32m    385\u001b[0m     \u001b[39m# data 就是数据表格，各个检测出来的bar的长度或者line高度，或者角度\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    399\u001b[0m     \u001b[39m# ]\u001b[39;00m\n\u001b[1;32m    400\u001b[0m     \u001b[39m# 目前要做的就是添加横轴纵轴说明\u001b[39;00m\n\u001b[1;32m    401\u001b[0m     plot_area, image_painted, data, chart_data \u001b[39m=\u001b[39m test(picturePath, word_infos)\n",
      "File \u001b[0;32m~/autodl-tmp/DeepRule/test_api.py:126\u001b[0m, in \u001b[0;36mocr_result\u001b[0;34m(image_path)\u001b[0m\n\u001b[1;32m    124\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mocr_result\u001b[39m(image_path):\n\u001b[1;32m    125\u001b[0m     ocr \u001b[39m=\u001b[39m PaddleOCR(use_angle_cls\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m, lang\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39mch\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[0;32m--> 126\u001b[0m     result \u001b[39m=\u001b[39m ocr\u001b[39m.\u001b[39;49mocr(image_path, \u001b[39mcls\u001b[39;49m\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)\n\u001b[1;32m    127\u001b[0m     word_infos \u001b[39m=\u001b[39m []\n\u001b[1;32m    128\u001b[0m     word_dict \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n",
      "File \u001b[0;32m~/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/paddleocr.py:370\u001b[0m, in \u001b[0;36mPaddleOCR.ocr\u001b[0;34m(self, img, det, rec, cls)\u001b[0m\n\u001b[1;32m    368\u001b[0m img, flag \u001b[39m=\u001b[39m check_and_read_gif(image_file)\n\u001b[1;32m    369\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m flag:\n\u001b[0;32m--> 370\u001b[0m     \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39;49m(image_file, \u001b[39m'\u001b[39;49m\u001b[39mrb\u001b[39;49m\u001b[39m'\u001b[39;49m) \u001b[39mas\u001b[39;00m f:\n\u001b[1;32m    371\u001b[0m         np_arr \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mfrombuffer(f\u001b[39m.\u001b[39mread(), dtype\u001b[39m=\u001b[39mnp\u001b[39m.\u001b[39muint8)\n\u001b[1;32m    372\u001b[0m         img \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mimdecode(np_arr, cv2\u001b[39m.\u001b[39mIMREAD_COLOR)\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'bar4.jpeg'"
     ]
    }
   ],
   "source": [
    "barPath = \"image/bar4.jpeg\"\n",
    "res = GetTableFromPicture(barPath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([], None, [[19.49, 22.67, 22.12, 16.91, 24.28], [23.35, 20.59, 22.13, 25.57, 17.14], [25.24, 24.82, 23.81, 25.59, 26.7]], [0, {0: [258.9734191894531, 479.2862548828125, 542.230712890625, 515.9313354492188, 0.6127422224260001], 1: [15.010784149169922, 196.69729614257812, 41.72893524169922, 300.73370361328125, 0.5305352067159409], 4: [39.157737731933594, 28.698196411132812, 785.898193359375, 468.64361572265625, 0.8069283988253129], 5: [91.11587524414062, 60.23095703125, 786.0899047851562, 435.8214111328125, 0.8665542898595067]}, {1: '水果消费总量 '}, 29, 10.0])\n"
     ]
    }
   ],
   "source": [
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"./image/testBar.png\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from test_api import GetTableFromPicture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(benchmark=False, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/2.4/ocr/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/2.4/ocr/det/ch/ch_PP-OCRv2_det_infer', det_pse_box_thresh=0.85, det_pse_box_type='box', det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, gpu_mem=500, help='==SUPPRESS==', image_dir=None, ir_optim=True, label_list=['0', '180'], label_map_path='./vqa/labels/labels_ser.txt', lang='ch', layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', max_batch_size=10, max_seq_length=512, max_text_length=25, min_subgraph_size=15, mode='structure', model_name_or_path=None, ocr_version='PP-OCRv2', output='./output', precision='fp32', process_id=0, rec=True, rec_algorithm='CRNN', rec_batch_num=6, rec_char_dict_path='/root/miniconda3/envs/ocr/lib/python3.8/site-packages/paddleocr/ppocr/utils/ppocr_keys_v1.txt', rec_image_shape='3, 32, 320', rec_model_dir='/root/.paddleocr/2.4/ocr/rec/ch/ch_PP-OCRv2_rec_infer', save_crop_res=False, save_log_path='./log_output/', show_log=True, structure_version='STRUCTURE', table_char_dict_path=None, table_char_type='en', table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=True, use_dilation=False, use_gpu=False, use_mp=False, use_onnx=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-27 11:19:42,570] [   DEBUG] predict_system.py:70 - dt_boxes num : 14, elapse : 0.13508224487304688\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/27 11:19:42] root DEBUG: dt_boxes num : 14, elapse : 0.13508224487304688\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-27 11:19:42,638] [   DEBUG] predict_system.py:85 - cls num  : 14, elapse : 0.06080913543701172\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/27 11:19:42] root DEBUG: cls num  : 14, elapse : 0.06080913543701172\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-03-27 11:19:42,837] [   DEBUG] predict_system.py:89 - rec_res num  : 14, elapse : 0.19652915000915527\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024/03/27 11:19:42] root DEBUG: rec_res num  : 14, elapse : 0.19652915000915527\n",
      "tensor([[8.3252, 0.0000, 0.0000]], device='cuda:0')\n",
      "25.0\n",
      "10.0\n",
      "0.2471346385915146\n",
      "Predict the lower bar\n",
      "Predicted as BarChart\n",
      "Xs [{'text': '橘子', 'boundingBox': [283.0, 447.0, 0, 0, 316.0, 465.0]}, {'text': '梨', 'boundingBox': [429.0, 447.0, 0, 0, 447.0, 464.0]}, {'text': '苹果', 'boundingBox': [144.0, 448.0, 0, 0, 175.0, 463.0]}, {'text': '葡萄', 'boundingBox': [561.0, 447.0, 0, 0, 596.0, 464.0]}, {'text': '香蕉', 'boundingBox': [701.0, 449.0, 0, 0, 731.0, 464.0]}]\n",
      "filtedXs []\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "sort() takes no positional arguments",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m res  \u001b[39m=\u001b[39m GetTableFromPicture(path)\n",
      "File \u001b[0;32m~/autodl-tmp/DeepRule/test_api.py:411\u001b[0m, in \u001b[0;36mGetTableFromPicture\u001b[0;34m(picturePath)\u001b[0m\n\u001b[1;32m    409\u001b[0m Yi \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n\u001b[1;32m    410\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m4\u001b[39m \u001b[39min\u001b[39;00m chart_data[\u001b[39m1\u001b[39m]\u001b[39m.\u001b[39mkeys():\n\u001b[0;32m--> 411\u001b[0m     Xs \u001b[39m=\u001b[39m getXAxisList(word_infos, chart_data[\u001b[39m1\u001b[39;49m][\u001b[39m4\u001b[39;49m])\n\u001b[1;32m    412\u001b[0m     Yi \u001b[39m=\u001b[39m getYAxisInstruction(word_infos,chart_data[\u001b[39m1\u001b[39m][\u001b[39m4\u001b[39m])\n\u001b[1;32m    413\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mlen\u001b[39m(Xs) \u001b[39m==\u001b[39m \u001b[39m0\u001b[39m:\n",
      "File \u001b[0;32m~/autodl-tmp/DeepRule/test_api.py:339\u001b[0m, in \u001b[0;36mgetXAxisList\u001b[0;34m(word_infos, bbox)\u001b[0m\n\u001b[1;32m    337\u001b[0m \u001b[39m# 按照左上角横坐标进行排序\u001b[39;00m\n\u001b[1;32m    338\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mfiltedXs\u001b[39m\u001b[39m\"\u001b[39m, filtedXs)\n\u001b[0;32m--> 339\u001b[0m filtedXs\u001b[39m.\u001b[39;49msort(\u001b[39mlambda\u001b[39;49;00m a:a[\u001b[39m\"\u001b[39;49m\u001b[39mboundingBox\u001b[39;49m\u001b[39m\"\u001b[39;49m][\u001b[39m0\u001b[39;49m])\n\u001b[1;32m    340\u001b[0m \u001b[39mreturn\u001b[39;00m filtedXs\n",
      "\u001b[0;31mTypeError\u001b[0m: sort() takes no positional arguments"
     ]
    }
   ],
   "source": [
    "res  = GetTableFromPicture(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([],\n",
       " None,\n",
       " [[19.49, 22.67, 22.12, 16.91, 24.28],\n",
       "  [23.35, 20.59, 22.13, 25.57, 17.14],\n",
       "  [25.24, 24.82, 23.81, 25.59, 26.7]],\n",
       " [0,\n",
       "  {0: [258.9734191894531,\n",
       "    479.2862548828125,\n",
       "    542.230712890625,\n",
       "    515.9313354492188,\n",
       "    0.6127422224260001],\n",
       "   1: [15.010784149169922,\n",
       "    196.69729614257812,\n",
       "    41.72893524169922,\n",
       "    300.73370361328125,\n",
       "    0.5305352067159409],\n",
       "   4: [39.157737731933594,\n",
       "    28.698196411132812,\n",
       "    785.898193359375,\n",
       "    468.64361572265625,\n",
       "    0.8069283988253129],\n",
       "   5: [91.11587524414062,\n",
       "    60.23095703125,\n",
       "    786.0899047851562,\n",
       "    435.8214111328125,\n",
       "    0.8665542898595067]},\n",
       "  {1: '水果消费总量 '},\n",
       "  29,\n",
       "  10.0])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
